package org.scrm.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.scrm.component.ChannelService;
import org.scrm.dao.ProductDao;
import org.scrm.eum.BshSmsTypeEnum;
import org.scrm.eum.ChannelEnum;
import org.scrm.model.BshSmsParams;
import org.scrm.model.Product;
import org.scrm.model.SmsResult;
import org.scrm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;
    @Autowired
    ChannelService channelService;

    @Override
    public Product findByPid(Integer pid) {
        return productDao.findById(pid).get();
    }

    @Override
    public String smsSend(String mobile) throws InterruptedException {
       // mobile = "15093498965";
        mobile = "18738919772";
        boolean sendCodeResult = true;
       // String code = CommonUtils.generate();
       // String content = "您的验证码是："+ code +" 请勿转发。";
       // String content = "您的验证码是：请勿转发。";
       String content =
        "您的验证码是：1351 请勿转发  ";


        BshSmsParams bshSmsParams = new BshSmsParams();
        bshSmsParams.setChannelEnum(ChannelEnum.VERIFICATION_CODE);
        bshSmsParams.setBshSmsTypeEnum(BshSmsTypeEnum.SINGLE);
        bshSmsParams.setMobile(mobile);
        bshSmsParams.setMessage(content);
        SmsResult result =null;
        JSONObject jsonObject = new JSONObject();
        boolean flag = true ;
        Integer i = 0;
        while (flag){
            Thread.sleep(60);
            log.info("执行第"+ i +"次，内容 content "+ content + "手机号 mobile " + mobile);
            result = channelService.single(mobile, content, bshSmsParams.getSerialNumber());
            boolean sendResult = "0".equals(result.getResult());
            i++;
            log.info("result ==== {}", result);
            if (!"0".equals(result.getResult())){
                flag = false;
            }
        }
        log.info("执行完成");
        return result.getResult();
    }
}
